<template>
  <a-modal
    :maskClosable="false"
    :keyboard="false"
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">

    <a-spin :spinning="confirmLoading">
      <a-form>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.message.entity.templateName')">
          <a-input disabled v-model="templateName"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.message.entity.templateContent')">
          <a-textarea disabled v-model="templateContent" :autoSize="{ minRows: 5, maxRows: 8 }"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.message.entity.msgBody')">
          <a-textarea :placeholder="$t('m.system.message.verify.esParam')" v-model="msgBody" :autoSize="{ minRows: 5, maxRows: 8 }"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.message.entity.esType')">
         <a-radio-group  v-model="msgType" button-style="solid">
           <a-radio-button value="2">{{$t('m.system.message.other.esType_option2')}}</a-radio-button>
           <a-radio-button value="1">{{$t('m.system.message.other.esType_option1')}}</a-radio-button>
           <a-radio-button v-if="showDingtalk" value="5">{{$t('m.system.message.other.esType_option5')}}</a-radio-button>
         </a-radio-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('m.system.message.entity.esReceiver')">
          <a-input placeholder="" v-model="receiver"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import {httpAction} from '@/api/manage'
  import YchConstant from "@/api/YchConstant";
  import config from '@/settings'

  export default {
    name: "SysMessageTestModal",
    data() {
      return {
        showDingtalk: config.showDingtalk,
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },

        confirmLoading: false,
        url: {
          send: YchConstant.PREFIX_SYSTEM + "/message/sysMessageTemplate/sendMsg",
        },
        templateName: "",
        templateContent: "",
        receiver: "",
        msgType: "",
        msgBody: "",
        sendParams: {}
      }
    },
    methods: {
      open(record) {
        this.sendParams.templateCode = record.templateCode;
        this.templateName = record.templateName;
        this.templateContent = record.templateContent;
        this.msgBody = record.templateTestJson;
        this.visible = true;
      },
      close() {
        this.receiver = "";
        this.msgType = "";
        this.sendParams = {};
        this.visible = false;
      },
      handleOk() {
        let httpurl = this.url.send;
        let method = 'post';
        this.sendParams.msgBody = this.msgBody;
        this.sendParams.receiver = this.receiver;
        this.sendParams.msgType = this.msgType;
        httpAction(httpurl, this.sendParams, method).then((res) => {
          if (res.success) {
            this.messageUtil.success(res.message);
          } else {
            this.messageUtil.warning(res.message);
          }
        }).finally(() => {
          this.confirmLoading = false;
          this.close();
        })
      },
      handleCancel() {
        this.close()
      },
    }
  }
</script>
